package com.nurese.controller;

import com.nurese.entity.Patient;
import com.nurese.entity.Patient;
import com.nurese.entity.User;
import com.nurese.service.PatientService;
import com.nurese.service.PatientService;
import com.nurese.service.UserService;
import com.nurese.utils.MapControll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/patient")
public class PatientController {

    private final String LIST="Patient/list";
    private final String ADD="Patient/add";
    private final String UPDATE="Patient/update";



    @Autowired
    private PatientService patientService;
    @Autowired
    private UserService userService;

    @GetMapping("/add")
    public String create(ModelMap modelMap){
        System.out.println("+++ 添加");
        List<User> users=userService.query(null);
        modelMap.addAttribute("users",users);
        return ADD;
    }

    @PostMapping("/create")
    @ResponseBody
//    public void  create(Patient patient){
//        patientService.create(patient);
//    }
    public Map<String,Object> create(Patient patient){
        System.out.println("+++创建");
//        patientService.create(patient);
        int result= patientService.create(patient);
        if (result<=0){
//            新增失败
            return MapControll.getInstance().error().getMap();

        }
        return MapControll.getInstance().success().getMap();
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        int result = patientService.delete(id);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }
    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(String ids){
        int result = patientService.delete(ids);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @PostMapping("/update")
    @ResponseBody
//    @RequestBody
    public Map<String,Object> update(Patient patient){
        System.out.println("======22"+patient);
        int result = patientService.update(patient);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @GetMapping ("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap modelMap){

        Patient patient=patientService.detail(id);
        modelMap.addAttribute("patient",patient);
        List<User> users=userService.query(null);
        modelMap.addAttribute("users",users);
        return UPDATE;
    }


    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(Patient patient){
        List<Patient> list =patientService.query(patient);
        List<User> users=userService.query(null);
        list.forEach(entity ->{
            users.forEach(user -> {
                if(entity.getUserid()==user.getId().intValue()){
                    entity.setUser(user);
                }
            });
        } );
        Integer count=patientService.count(patient);
        return MapControll.getInstance().success().put("data",list).put("count",count).getMap();
    }

    @GetMapping("/list")
    public String list(){

        return LIST;
    }
}


